package learning.spring.family.mybatispagehelperdemo;

import com.github.pagehelper.PageInfo;
import learning.spring.family.mybatispagehelperdemo.mapper.CoffeeMapper;
import learning.spring.family.mybatispagehelperdemo.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@SpringBootApplication
@Slf4j
@MapperScan("learning.spring.family.mybatispagehelperdemo.mapper")
public class MybatisPagehelperDemoApplication implements ApplicationRunner {
    @Autowired
    private CoffeeMapper coffeeMapper;

    public static void main(String[] args) {
        SpringApplication.run(MybatisPagehelperDemoApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("===========RowBounds====");
        coffeeMapper.findAllWithRowBounds(new RowBounds(1, 3)).forEach(k -> log.info("Page(1) Coffee:{}", k));
        coffeeMapper.findAllWithRowBounds(new RowBounds(2, 3)).forEach(k -> log.info("Page(2) Coffee:{}", k));
        log.info("===========RowBounds====");
        coffeeMapper.findAllWithRowBounds(new RowBounds(1, 0)).forEach(k -> log.info("Page(1) Coffee:{}", k));
        log.info("=========Pagehelper==========");
        coffeeMapper.findAllWithParam(1, 3).forEach(k -> log.info("Page(1) Coffee:{}", k));
        List<Coffee> coffeeList = coffeeMapper.findAllWithParam(2, 3);
        coffeeList.forEach(k -> log.info("Page(2) Coffee:{}", k));
        PageInfo page = new PageInfo(coffeeList);
        log.info("pageInfo:{}", page);
    }
}
